package com.tyt.travel.common.util;

import com.tyt.travel.enums.FileTypeEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author tianyun
 * @version 1.0
 * @date Created in 2021年02月10日 20:05
 * @since 1.0
 */
@Component
public class IOUtil {

    @Value("${file.path}")
    private String filepath;
    @Value("${file.url}")
    private String fileUrl;

    public String uploadFile(InputStream inputStream,String fileName){
        String path = filepath + fileName;
        String url = fileUrl + fileName;
        File file = new File(path);

        if (file.exists()){
            return url;
        }

        try (FileOutputStream outputStream = new FileOutputStream(file)){
            org.apache.commons.io.IOUtils.copy(inputStream,outputStream);
            byte[] bytes = new byte[2048];
            outputStream.write(bytes);
            return url;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
